package com.xugou.controller;

import com.xugou.pojo.Emp;
import com.xugou.pojo.Notice;
import com.xugou.pojo.User;
import com.xugou.service.EmpService;
import com.xugou.service.NoticeService;
import com.xugou.utils.Contants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/notice")
public class NoticeController {

    static Integer updateId;

    @Autowired
    @Qualifier("noticeServiceImpl")
    private NoticeService noticeService;

    @Autowired
    @Qualifier("empServiceImpl")
    private EmpService empService;

    @RequestMapping("/allnotice")
    //获取全部公告
    public String getAllNotice(HttpServletRequest model){
        String msg = "";
        List<Notice> noticeList = noticeService.getNoticeList();
        model.setAttribute("noticelist",noticeList);
        if (noticeList==null){
            msg = "error";
            model.setAttribute("msg",msg);
            return "/notice/notice_index";
        }
        return "/notice/notice_index";
    }

    //查看公告具体信息
    @RequestMapping("/toQueryThis/{noticeId}")
    public String toQueryThis(@PathVariable("noticeId")int id,Model model){

        //获取公告id
        Notice notice = noticeService.getNoticeById(id);
        //获取员工姓名
        Integer empId = notice.getEmpId();
        Emp emp = empService.queryEmpId(empId);
        String empName = emp.getEmpName();

        model.addAttribute("notice",notice);
        model.addAttribute("empName",empName);
        return "/notice/noticeInfo";
    }

    //跳转到修改公告
    @RequestMapping("/toUpdateThis/{noticeId}")
    public String toUpdateThis(@PathVariable("noticeId")int id,Model model){

        Notice notice = noticeService.getNoticeById(id);
        updateId=notice.getNoticeId();
        model.addAttribute("noticeUpdate",notice);
        return "/notice/noticeUpdateInfo";
    }
    //修改
    //修改详细公告
    @RequestMapping("/updateNoticeInfo")
    public String updateNoticeInfo(Notice notice, Model model){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //补全公告信息
        notice.setNoticeId(updateId);
        notice.setNoticePubtime(dateFormat.format(date));
        System.out.println(notice);
        boolean b = noticeService.updateNotice(notice);
        return "redirect:/notice/allnotice";
    }
    //删除公告
    @RequestMapping("/delThis/{noticeId}")
    public String deleteNotice(@PathVariable("noticeId") int id){
        noticeService.deleteNotice(id);
        return "redirect:/notice/allnotice";
    }

    //跳转新增公告
    @RequestMapping("/toAddNotice")
    public String toAddNotice(){
        return "notice/addNotice";
    }

    //新增公告
    @RequestMapping("/addNotice")
    public String addNotice(Notice notice,HttpServletRequest req) {

        String msg = "";

        //获取发布人信息
        User emp = (User)req.getSession().getAttribute(Contants.USER_SEESION);
        Integer empId = emp.getEmpId();
        //获取当前发布时间
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //补全公告信息
        notice.setNoticePubtime(dateFormat.format(date));
        notice.setEmpId(empId);
        //添加
        boolean b = noticeService.addNotice(notice);
        if (b) {
            msg = "添加成功";
            return "redirect:/notice/allnotice";
        } else {
            msg = "添加失败";
            req.setAttribute("msg",msg);
            return "redirect:/notice/toAddNotice";
        }

    }

}
